package cn.snmp.mutual.nms;

import java.io.*;

public class SnmpVarBind implements Serializable
{
    private SnmpOID m_oid;
    private SnmpVar m_variable;
    private int m_varbindErrIndex;
    
    SnmpVarBind() {
        this.m_oid = null;
        this.m_variable = null;
        this.m_varbindErrIndex = 0;
    }
    
    public SnmpVarBind(final SnmpOID snmpoid, final SnmpVar snmpvar) {
        this.m_oid = null;
        this.m_variable = null;
        this.m_varbindErrIndex = 0;
        this.m_oid = snmpoid;
        this.m_variable = snmpvar;
        if (snmpvar == null) {
            this.m_variable = new SnmpNull();
        }
    }
    
    int encode(final byte[] bytes, int i) throws ArrayIndexOutOfBoundsException {
        final int j = i;
        if (this.m_variable == null) {
            i = ASNTypes.encodeNull(bytes, i, 5);
        }
        else {
            i = this.m_variable.encode(bytes, i);
        }
        i = this.m_oid.encode(bytes, i);
        i = ASNTypes.encodeSequence(bytes, i, j - i, 48);
        return i;
    }
    
    @Override
    public boolean equals(final Object object) {
        if (object == null || !(object instanceof SnmpVarBind)) {
            return false;
        }
        final SnmpVarBind snmpVarBind = (SnmpVarBind)object;
        return this == snmpVarBind || (this.m_oid == null && snmpVarBind.getObjectID() == null && this.m_variable == null && snmpVarBind.getVariable() == null) || ((this.m_oid != null || snmpVarBind.getObjectID() == null) && (this.m_oid == null || snmpVarBind.getObjectID() != null) && (this.m_variable != null || snmpVarBind.getVariable() == null) && (this.m_variable == null || snmpVarBind.getVariable() != null) && this.m_variable.getType() == snmpVarBind.getVariable().getType() && this.m_oid.equals(snmpVarBind.getObjectID()) && this.m_variable.equals(snmpVarBind.getVariable()));
    }
    
    public int getErrIndex() {
        return this.m_varbindErrIndex;
    }
    
    public void setErrIndex(final int varbindErrIndex) {
        this.m_varbindErrIndex = varbindErrIndex;
    }
    
    public SnmpOID getObjectID() {
        return this.m_oid;
    }
    
    public SnmpVar getVariable() {
        return this.m_variable;
    }
    
    public void setObjectID(final SnmpOID oid) {
        this.m_oid = oid;
    }
    
    public void setVariable(final SnmpVar var) {
        this.m_variable = var;
    }
    
    @Override
    public String toString() {
        String str = null;
        if (this.m_variable.Type == 2) {
            str = ((SnmpInt)this.m_variable).toString();
        }
        else {
            str = this.m_variable.toString();
        }
        if (this.m_variable.Type == 4 && (str.indexOf(0) > -1 || this.m_oid.toString().startsWith(".1.3.6.1.2.1.3.1.1.2") || this.m_oid.toString().startsWith(".1.3.6.1.2.1.4.22.1.2") || this.m_oid.toString().startsWith(".1.3.6.1.2.1.2.2.1.6"))) {
            str = ((SnmpString)this.m_variable).toByteString();
        }
        if (this.m_oid == null || this.m_variable == null) {
            return null;
        }
        return this.m_oid.toString() + ": " + str;
    }
    
    public String toTagString() {
        String tagStr = null;
        if (this.m_variable.Type == 2) {
            tagStr = ((SnmpInt)this.m_variable).toTagString();
        }
        else {
            tagStr = this.m_variable.toTagString();
        }
        if (this.m_variable.Type == 4) {
            if (this.m_oid.toString().startsWith(".1.3.6.1.2.1.3.1.1.2") || this.m_oid.toString().startsWith(".1.3.6.1.2.1.4.22.1.2") || this.m_oid.toString().startsWith(".1.3.6.1.2.1.2.2.1.6")) {
                tagStr = "PhysAddress: " + ((SnmpString)this.m_variable).toByteString();
            }
            else if (tagStr.indexOf(0) != -1) {
                tagStr = "STRING: " + ((SnmpString)this.m_variable).toByteString();
            }
        }
        if (this.m_oid == null || this.m_variable == null) {
            return null;
        }
        return this.m_oid.toTagString() + "\n" + tagStr;
    }
}
